package com.xored.glance.ui.viewers.tree;

import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TreeViewer;

public abstract class TreeLabelProvider {

	public static TreeLabelProvider create(TreeViewer viewer) {
		IBaseLabelProvider provider = viewer.getLabelProvider();
		if (provider instanceof ILabelProvider) {
			return new SimpleTreeLabelProvider((ILabelProvider) provider);
		} else if (provider instanceof ITableLabelProvider) {
			return new TableTreeLabelProvider((ITableLabelProvider) provider);
		}
		return null;
	}

	public abstract String getText(Object element, int column);

	public static class SimpleTreeLabelProvider extends TreeLabelProvider {

		ILabelProvider provider;

		public SimpleTreeLabelProvider(ILabelProvider provider) {
			this.provider = provider;
		}

		@Override
		public String getText(Object element, int column) {
			return provider.getText(element);
		}

	}

	public static class TableTreeLabelProvider extends TreeLabelProvider {

		ITableLabelProvider provider;

		public TableTreeLabelProvider(ITableLabelProvider provider) {
			this.provider = provider;
		}

		@Override
		public String getText(Object element, int column) {
			return provider.getColumnText(element, column);
		}

	}

}
